package com.bytedance.improvingroad.activity

import android.view.ContextMenu
import android.view.MenuItem
import android.view.View
import android.widget.PopupMenu
import androidx.annotation.MenuRes
import androidx.core.util.Pair
import androidx.customview.widget.ViewDragHelper
import com.bytedance.improvingroad.R
import com.bytedance.improvingroad.databinding.ActivityCardBinding
import com.google.android.material.card.MaterialCardView
import com.google.android.material.datepicker.MaterialDatePicker
import com.google.android.material.timepicker.MaterialTimePicker
import com.google.android.material.timepicker.MaterialTimePicker.INPUT_MODE_CLOCK
import com.google.android.material.timepicker.MaterialTimePicker.INPUT_MODE_KEYBOARD
import com.google.android.material.timepicker.TimeFormat

class CardActivity : BaseBindingActivity<ActivityCardBinding>() {
    override fun initData() {

    }

    override fun initListener() {
        binding.card.setOnLongClickListener {
            binding.card.apply {
                isChecked = !isChecked
            }
            true
        }
//        showRangedDialog()
//        showNoRangeDialog()
        registerForContextMenu(binding.menuTv)
        binding.btnMenu.setOnClickListener {
            showMenu(binding.btnMenu,R.menu.contextual_action_bar)
        }
        showTimePickerDialog()
    }

    private fun showMenu(v: View, @MenuRes menuRes: Int) {
        val popup = PopupMenu(this, v)
        popup.menuInflater.inflate(menuRes, popup.menu)

        popup.setOnMenuItemClickListener { menuItem: MenuItem ->
            when(menuItem.itemId) {
                R.id.share -> {
                    true
                }
                R.id.delete -> {
                    true
                }
                R.id.more -> {
                    true
                }
                else -> false
            }
        }
        popup.setOnDismissListener {
        }
        // Show the popup menu.
        popup.show()
    }

    override fun onCreateContextMenu(
        menu: ContextMenu?,
        v: View?,
        menuInfo: ContextMenu.ContextMenuInfo?
    ) {
        super.onCreateContextMenu(menu, v, menuInfo)
        menuInflater.inflate(R.menu.contextual_action_bar,menu)
    }

    override fun onContextItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.share -> {
                true
            }
            R.id.delete -> {
                true
            }
            R.id.more -> {
                true
            }
            else -> super.onContextItemSelected(item)
        }
    }
    fun showNoRangeDialog() {
        val datePicker = MaterialDatePicker.Builder.datePicker()
            .setTitleText("Select date")
            .setSelection(MaterialDatePicker.todayInUtcMilliseconds())
            .setInputMode(MaterialDatePicker.INPUT_MODE_TEXT)
            .build()

        datePicker.show(supportFragmentManager,"tag")
        datePicker.addOnPositiveButtonClickListener {
            // 确定按钮
        }
        datePicker.addOnNegativeButtonClickListener {
            // 否定按钮
        }
        datePicker.addOnCancelListener {
            // 取消按钮
        }
        datePicker.addOnDismissListener {
            // 关闭按钮
        }
    }

    fun showRangedDialog() {
        MaterialDatePicker.Builder.dateRangePicker()
            .setTitleText("Select dates")
            .setSelection(
                Pair(
                    MaterialDatePicker.thisMonthInUtcMilliseconds(),
                    MaterialDatePicker.todayInUtcMilliseconds()
                )
            )
            .build()
            .show(supportFragmentManager,"tag")
    }

    private fun showTimePickerDialog() {
        val picker = MaterialTimePicker.Builder()
                //设置小时制，12小时的界面会不同
                .setTimeFormat(TimeFormat.CLOCK_24H)
                .setHour(12)
                .setMinute(10)
                .setTitleText("Select Appointment time")
                //设置展示的输入方式，不设置也可以，两种模式可以手动切换
                .setInputMode(INPUT_MODE_CLOCK)
                .build()
        picker.addOnPositiveButtonClickListener {
            // 点击确定按钮
        }
        picker.addOnNegativeButtonClickListener {
            // 点击取消按钮
        }
        picker.addOnCancelListener {
            // call back code
        }
        picker.addOnDismissListener {
            // Dialog消失的回调
        }
        picker.show(supportFragmentManager,"tag")
    }

    private inner class ViewDragHelperCallback : ViewDragHelper.Callback() {

        override fun onViewCaptured(capturedChild: View, activePointerId: Int) {
            (capturedChild as? MaterialCardView).let { materialCardView ->
                materialCardView?.isDragged = true
            }
        }

        override fun onViewReleased(releaseChild: View, xVel: Float, yVel: Float) {
            (releaseChild as? MaterialCardView).let { materialCardView ->
                materialCardView?.isDragged = false
            }
        }

        override fun tryCaptureView(child: View, pointerId: Int): Boolean {
            return true
        }
    }
}